#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   dns_service.sh
# Version:    1.0
# Date:       2013/10/16
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-06DNS-01字符模式-01DNS服务控制与信息查询-01DNS服务的启动和停止
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/10/16
#             - Start named service.
# ----------------------------------------------------------------------

##! @TODO: start&stop named service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    serv=named

    exists ${serv} rndc-confgen
    EchoResult "check commands"
    
    CONF=/etc/named.conf
    [ -f ${CONF} ]
    EchoResult "check ${CONF} exists"
    OWNER=`ls -l ${CONF} | awk '{print $3,$4}'`
    if [ "${OWNER}" != "named named" ]; then
        chown named:named ${CONF}
        EchoResult "check owner of ${CONF}"
    fi

    RNDCKEY="/etc/rndc.key"
    if [ ! -f ${RNDCKEY} ];then
        rndc-confgen -r /dev/urandom -a
        [ -f ${RNDCKEY} ]
        EchoResult "check ${RNDCKEY}"
    fi
        

    OWNER=`ls -l ${RNDCKEY} | awk '{print $3,$4}'`
    if [ "${OWNER}" != "named named" ]; then
        chown named:named ${RNDCKEY}
        EchoResult "check owner of ${RNDCKEY}"
    fi


    status_daemon ${serv}

    # start
    if [ $? -ne 0 ]
    then
        echo "${serv} is stopped"
        start_daemon ${serv}
    else
        echo "${serv} is running..."
    fi 

    sleep 3
    # check httpd start
    status_daemon ${serv}  && netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} start"

    # restart
    restart_daemon ${serv}

    sleep 3
    # check httpd restart
    status_daemon ${serv}  && netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} restart"

    # stop
    stop_daemon ${serv}

    # check httpd stop
    ! netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} stop"

}


# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh

do_test || EchoResult "testcase: ${serv} start,stop,restart"
echo ""
